package top.kpromise.note.modules.todo.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;
import top.kpromise.common.base.PageResult;
import top.kpromise.common.base.Result;
import top.kpromise.note.base.BaseApi;
import top.kpromise.note.modules.todo.entity.ToDoEntity;
import top.kpromise.note.modules.todo.model.CreateToDo;
import top.kpromise.note.modules.todo.service.ToDoService;

import java.util.Date;
import java.util.Optional;

@RestController
@Api(tags = {"待办事项"})
@RequestMapping("/api/todo")
public class ToDo extends BaseApi {

    private final ToDoService toDoService;

    public ToDo(ToDoService toDoService) {
        this.toDoService = toDoService;
    }

    @RequestMapping(value = "/put", method = RequestMethod.PUT)
    @ApiOperation(value = "新建待办")
    @ApiImplicitParam(name = "createToDo", dataType = "CreateToDo",
            paramType = "body", required = true)
    public Result<String> createToDo(@RequestBody CreateToDo createToDo) {
        createToDo.setUserId(userInfo().getId());
        toDoService.saveToDo(createToDo);
        return Result.success("新建成功");
    }

    @RequestMapping(value = "/finish/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "完成待办")
    @ApiImplicitParam(name = "id", dataType = "long", value = "待办ID",
            paramType = "path", required = true)
    public Result<String> finish(@PathVariable Long id) {
        Optional<ToDoEntity> toDoEntity = toDoService.findById(id);
        if (!toDoEntity.isPresent()) {
            return Result.error("该TODO不存在！");
        }
        ToDoEntity updateItem = toDoEntity.get();
        updateItem.setFinishAt(new Date());
        updateItem.setState(ToDoEntity.STATE_FINISHED);
        toDoService.save(updateItem);
        return Result.success("待办完成成功");
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ApiOperation(value = "列出所有的待办")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页码", dataType = "int",
                    paramType = "query", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int",
                    paramType = "query", example = "20", required = true)
    })
    public PageResult<ToDoEntity> listAll(int pageSize, int pageNo) {
        return list(toDoService, pageSize, pageNo);
    }

    @RequestMapping(value = "/listFinished", method = RequestMethod.GET)
    @ApiOperation(value = "列出已完成的待办")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页码", dataType = "int",
                    paramType = "query", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int",
                    paramType = "query", example = "20", required = true)
    })
    public PageResult<ToDoEntity> listFinished(int pageSize, int pageNo) {
        return list(pageSize, pageNo, ToDoEntity.STATE_FINISHED);
    }

    @RequestMapping(value = "/listOngoing", method = RequestMethod.GET)
    @ApiOperation(value = "列出进行中的待办")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页码", dataType = "int",
                    paramType = "query", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int",
                    paramType = "query", example = "20", required = true)
    })
    public PageResult<ToDoEntity> listOngoing(int pageSize, int pageNo) {
        return list(pageSize, pageNo, ToDoEntity.STATE_ON_GOING);
    }

    private PageResult<ToDoEntity> list(int pageSize, int pageNo, int state) {
        if (state < ToDoEntity.STATE_CREATED) state = ToDoEntity.STATE_CREATED;
        if (state > ToDoEntity.STATE_FINISHED) state = ToDoEntity.STATE_FINISHED;
        ToDoEntity toDoEntity = new ToDoEntity();
        toDoEntity.setState(state);
        return list(toDoService, pageSize, pageNo, Example.of(toDoEntity));
    }
}
